package com.nmh.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nmh.config.R;
import com.nmh.entity.Enclosure;
import com.nmh.entity.EnclosureAndPasture;
import com.nmh.mapper.EnclosureAndPastureMapper;
import com.nmh.mapper.EnclosureMapper;
import com.nmh.service.EnclosureAndPastureService;
import com.nmh.service.EnclosureService;
import com.nmh.service.PastureService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yoki
 * @since 2023-12-19
 */
@Api(tags="圈舍模块")
@RestController
@RequestMapping("/enclosures")
public class EnclosureController {
    //查询圈舍
    @Autowired
    private EnclosureService enclosureService;
    @Autowired
    private PastureService pastureService;
    @Autowired
    private EnclosureAndPastureMapper encmapper;
    @Autowired
    private EnclosureAndPastureService eapservice;
    @Autowired
    private EnclosureMapper enclosureMapper;
    @ApiOperation("圈舍新增")
    @PostMapping
    public R<String> insert(@ApiParam  @RequestBody Enclosure enclosure) {
        boolean add = enclosureService.add(enclosure);
        if (add)
            return R.success("添加成功");
        return R.error("已经有圈舍名","401");
    }
    @ApiOperation("/enclosures")
    @GetMapping
    public R<Page<EnclosureAndPasture>> getall(int pageNumber,int pageSize) {
        QueryWrapper<EnclosureAndPasture> encquery=new QueryWrapper<>();
        Page<EnclosureAndPasture> page = new Page<>(pageNumber,pageSize); // 查询第1页，每页10条数据
        encmapper.selectOrderPage(page,encquery);
        return R.success(page);
    }
    /*
    修改整体
     */
    @ApiOperation("修改圈舍")
    @PutMapping
    public R<String> modify(@RequestBody Enclosure enclosure) {
        Enclosure enclosure1 = enclosureMapper.selectByCircleName(enclosure);
        if (enclosure1 == null) {
            enclosureService.updateById(enclosure);
            return R.success("修改成功");
        }
        return R.error("修改失败,名字重复","401");
    }
    @ApiOperation("删除圈舍")
    @DeleteMapping
    public R<String> moveone(@RequestBody Enclosure enclosure) {
        enclosureService.removeById(enclosure);
        return R.error("删除成功","401");
    }
    /*
修改状态
 */
    @ApiOperation("修改状态")
    @PutMapping("/modifyState")
    public R<String> modifyState(@RequestBody Enclosure enclosure) {
       enclosureService.modifyByState(enclosure);
        return R.success("修改成功");
    }
    @ApiOperation("模糊查询")
    @PostMapping("/getByT")
    public R<Page<Enclosure>> getByT(@RequestBody Enclosure eap, int pageNumber,int pageSize ){
        Page<Enclosure> pages=new Page<>(pageNumber,pageSize);
        QueryWrapper<Enclosure> queryWrapper=new QueryWrapper<>();
        if(eap.equals("\"\"")){
            queryWrapper.like(StringUtils.isNotEmpty(eap.getCircleName()),"circle_name",eap.getCircleName())
                    .in("pasture_id",eap.getPastureId());
            enclosureService.page(pages,queryWrapper);
            return R.success(pages);
        }
        queryWrapper.like(StringUtils.isNotEmpty(eap.getCircleName()),"circle_name",eap.getCircleName())
                .like(null!=eap.getState(),"state",eap.getState())
                .eq("pasture_id",eap.getPastureId());
        System.out.println(eap);
        enclosureService.page(pages,queryWrapper);

        return R.success(pages);
    }
    /*
    根据圈舍id获取名称与存栏量
     */
    @ApiOperation("根据圈舍id获取名称与存栏量")
    @GetMapping("/ById")
    public R<Enclosure> getById(Integer id){
        Enclosure enclosure=enclosureService.getById(id);
        return R.success(enclosure);
    }

}

